//app.js
var WxParse = require('./wxParse/wxParse.js');
App({
	onLaunch: function () {
		//调用API从本地缓存中获取数据
		var logs = wx.getStorageSync('logs') || []
		logs.unshift(Date.now())
		wx.setStorageSync('logs', logs)
		// this.getUserInfo()
		// this.getIntB()
		// this.getOpenid()
	},


  getGoodData(catid,callback){
    var that = this;
    wx.request({
      url: 'https://xcxu.we-fs.com/index.php/api/goods/goodsInfo?admin_id=' + that.globalData.admin_id + '&type=' + catid + '&code=3',
      success:function(res){
        if (callback){
          callback(res.data.data[0])
        }
      }
    })
  },

  getlist(catid, callback){
    var that = this;
    wx.request({
      url: 'https://xcxu.we-fs.com/index.php/api/goods/classify?admin_id=' + that.globalData.admin_id + '&parent_id=' + catid + '&code=3',
      success: function (res) {
        if (callback) {
          callback(res.data.data)
        }
      }
    })
  },

	getUserInfo: function (cb) {
		var that = this
		if (this.globalData.userInfo) {
			typeof cb == "function" && cb(this.globalData.userInfo)
		} else {
			//调用登录接口
			wx.getUserInfo({
				withCredentials: false,
				success: function (res) {
					that.globalData.userInfo = res.userInfo
					typeof cb == "function" && cb(that.globalData.userInfo)
				}
			})
		}
	},

	checkUser: function (openid, thisPage, callback) {
		var that = this;
		wx.request({
			url: 'https://xcxu.we-fs.com/index.php/api/goods/checkUser?openid=' + openid + '&admin_id=' + that.globalData.admin_id,
			success: function (re) {
				console.log(re)
				if (re.data.code == 0) {
					that.globalData.vip = 1;
					that.globalData.vipinfo = re.data.data
					if (thisPage) {
						thisPage.setData({
							vipInfo: that.globalData.vipinfo
						})
					}
				} else {
					if (callback) {
						var fun = callback;
						fun()
					}
				}
			}
		})
	},

	getOpenid: function () {
		var that = this;
		wx.login({
			success: function (res) {
				if (res.code) {
					wx.request({
						url: 'https://xcxu.we-fs.com/index.php/api/goods/openid?code=' + res.code + '&appid=' + that.globalData.appid + '&secret=' + that.globalData.secret,
						success: function (re) {
							that.globalData.openid = re.data.openid;
							console.log(re)
						}
					})
				}
			}
		})
	},

	call: function () {
		var that = this;
		wx.makePhoneCall({
			phoneNumber: that.globalData.phone,
		})
	},

	go: function (lat, long) {
		var that = this;
		wx.getLocation({
			type: 'gcj02', //返回可以用于wx.openLocation的经纬度
			success: function (res) {
				wx.openLocation({
					latitude: hat.globalData.lat,
					longitude: hat.globalData.long,
					scale: 14,
					name: that.globalData.share
				})
			}
		});
	},

	showPop: function (msg, f) {
		if (f) {
			wx.showToast({
				title: msg,
			})
		} else {
			wx.showToast({
				title: msg,
				image: '../../images/error.png'
			})
		}
	},

	// value值
	// key表
	// state 加减
	opInt: function (value, key, state, callback, complete) {
		var that = this;
		wx.request({
			url: 'https://xcxu.we-fs.com/index.php/api/goods/integral?admin_id=' + that.globalData.admin_id + '&openid=' + that.globalData.openid + '&value=' + value + '&key=' + key + '&state=' + state,
			success: function (e) {
				console.log('余额积分')
				console.log(e)
				if (key == 'user_money') {
					that.showPop(e.data.msg, true)
				}
				if (callback) {
					var fun = callback;
					fun()
				}
			},
			complete: function () {
				if (complete) {
					var funn = complete;
					funn()
				}
			}
		})
	},

	payFor: function (name, phoneNo, price, goods_name, callback) {
		var that = this;
		wx.request({
			url: 'https://xcxu.we-fs.com/index.php/api/goods/zhifus?name=' + name + '&sj=' + phoneNo + '&openid=' + that.globalData.openid + '&price=' + price + '&goods_name=' + goods_name + '&mch_id=' + that.globalData.mch + '&appid=' + that.globalData.appid + '&secret=' + that.globalData.secret,
			data: {},
			method: 'GET',
			success: function (res) {
				console.log('rs====' + phoneNo);
				console.log('支付====' + name);

				wx.requestPayment(
					{
						'appId': that.globalData.appid,
						'timeStamp': res.data.timeStamp,
						'nonceStr': res.data.nonceStr,
						'package': 'prepay_id=' + res.data.prepayid,
						'signType': 'MD5',
						'paySign': res.data.sign,
						'success': function (res) {
							console.log(res)
							if (callback) {
								var fun = callback;
								fun()
							}
						},
						'fail': function (ress) {
							console.log(ress)
							that.showPop('支付失败', false)
						}
					})
			},
			fail: function () {
				wx.hideLoading()
				that.showPop('服务器错误', false)
			}
		})
	},

	getIntB: function (callback) {
		var that = this;
		wx.request({
			url: 'https://xcxu.we-fs.com/index.php/Api/admin/show?admin_id=' + that.globalData.admin_id,
			success: function (res) {
				console.log(res)
				that.globalData.intB = res.data.score_ratio;
				that.globalData.post_money = res.data.post_money;
				if (res.data.more_msg != '') {
					var arrObj = res.data.more_msg.split('-');
					console.log(arrObj)
					that.globalData.sw_int = arrObj[0]
					that.globalData.sw_vip = arrObj[1]
				}
				if (callback) {
					callback()
				}
			}
		})
	},

	globalData: {
		host: 'https://xcxu.we-fs.com/',
		openid: 'sssssssssxxsss',
		intB: null,
		goods_info: [],
		vipinfo: null,
		post_money: 0,
		sw_vip: 0,
		sw_int: 0,
		vip: '',
		userInfo: null,
		imageUrls: [],
    admin_id: 1170,  //admin_id
		share: '教师节贺卡', //程序名
		phone: '18888888888',
		lat: '',
		long: '',
		tell: '',
		name: '',
    pname:'',
    page:'',
    pday:'',
		appid: 'wxc0fde1304d2d44d6',//appid
		secret: '8e157fcb48af212873c68f6f4264eee0',//密钥
		mch: '1499209172',//商户号
		lunbo: 54664,//轮播
		classify: 54666,//首页分类
		huodong: 54669,//首页活动
		news: 54670,//首页最新
		shopclassify: 54671,//商城分类
		shopclassifyone: 54672,//商城第一个二级分类
	}
})
